$(function () {
    //页面加载完后，发出ajax请求
    $.post({
        url: baseurl+api.file.getImgInfo,
        xhrFields: {
            withCredentials: true
        },
        success: function (res) {
            var s="";
            var fileInfo=res.data;
            for (var i = 0; i < fileInfo.length; i++) {
                s+=" <tr>\n" +
                    "                            <td>"+fileInfo[i].key+"</td>\n" +
                    "                            <td>"+fileInfo[i].hash+"</td>\n" +
                    "                            <td>"+fileInfo[i].mimeType+"</td>\n" +
                    "                            <td>"+fileInfo[i].fsize+"</td>\n" +
                    "                            <td><a href='http://img.blognh.com/"+fileInfo[i].hash+"' target='_blank'>http://img.blognh.com/"+fileInfo[i].hash+"</a></td>\n" +
                    "                            <td colspan=\"6\"><button class=\"btn btn-danger\" onclick=\"deletefile('"+fileInfo[i].key+"')\">删除</button></td>\n" +
                    "                        </tr>";
            }
            $("#tbody").html(s);
        },
        datatype: "json"
    })
})

function deletefile(key) {
    swal({
        title: '确定删除吗?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定',
        cancelButtonText:"取消",
    }).then(function(isConfirm) {
        if (isConfirm) {
            $.post({
                url:baseurl+api.file.delete+key,
                xhrFields: {
                    withCredentials: true
                },
                success:function (res) {
                    swal(res.message);
                    if(res.code=="200"){
                        setTimeout(function () {
                            window.location.reload();
                        },1000)
                    }
                }
            })
        }
    })
}